﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Transactions;

namespace Songhay.Data.GenericWeb.Procedures
{
    using Songhay.Data.Common;
    using Songhay.Data.GenericWeb.DataTransferContracts;
    using Songhay.Data.GenericWeb.Properties;

    /// <summary>
    /// Insert procedures into the DBMS.
    /// </summary>
    public static partial class Insert
    {
        /// <summary>
        /// Inserts the specified Document data into the DBMS.
        /// </summary>
        /// <param name="commonConnection">The Common connection.</param>
        /// <param name="document">The Document data according to the data transfer contract.</param>
        /// <param name="documentSegments">The generic list of DocumentSegments according to the data transfer contract.</param>
        /// <param name="webKeywords">The generic list of WebKeywords according to the data transfer contract.</param>
        /// <returns>Returns the unique identifier of the inserted item.</returns>
        public static int? Document(DbConnection commonConnection, IDocument document, List<ISegmentDocument> documentSegments, List<IWebKeyword> webKeywords)
        {
            if(commonConnection == null) throw new ArgumentNullException("commonConnection", "The Common Connection is null.");
            if(document == null) throw new ArgumentNullException("document", "The Input Document is null.");

            Dictionary<string, object> paramSet = new Dictionary<string, object>();

            paramSet.Add("@SegmentId", document.SegmentId);
            paramSet.Add("@Title", document.Title);
            paramSet.Add("@DocumentShortName", document.DocumentShortName);
            paramSet.Add("@FileName", document.FileName);
            paramSet.Add("@Path", document.Path);
            paramSet.Add("@CreateDate", document.CreateDate);
            paramSet.Add("@ModificationDate", document.ModificationDate);
            paramSet.Add("@TemplateId", document.TemplateId);
            paramSet.Add("@IsRoot", document.IsRoot);
            paramSet.Add("@IsActive", document.IsActive);
            paramSet.Add("@SortOrdinal", document.SortOrdinal);
            paramSet.Add("@ClientId", document.ClientId);
            paramSet.Add("@Tag", document.Tag);

            object newId = null;
            using(TransactionScope tscope = new TransactionScope())
            {
                using(DbCommand cmd = commonConnection.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = Resources.InsertDocument;

                    DbParameter[] paramArray = CommonParameter.GetParameters(cmd, paramSet);
                    cmd.Parameters.AddRange(paramArray);

                    newId = cmd.ExecuteScalar();
                }

                foreach(ISegmentDocument documentSegment in documentSegments)
                {
                    Insert.SegmentDocument(commonConnection, documentSegment.SegmentId, newId);
                }

                foreach(IWebKeyword webKeyword in webKeywords)
                {
                    Insert.DocumentKeyword(commonConnection, newId, webKeyword.KeywordValue);
                }

                tscope.Complete();
            }

            return FrameworkType.ParseInt32(newId);
        }
    }
}
